package cn.com.header.web.controller.sys;


import cn.com.header.core.util.LangUtils;
import cn.com.header.core.util.Page;
import cn.com.header.web.model.sys.*;
import cn.com.header.web.service.sys.*;
import cn.com.header.web.vo.util.ZtreeVO;
import com.google.gson.Gson;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.security.auth.callback.LanguageCallback;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yyf on 2017/7/11.
 */
@Controller
@RequestMapping(value = "sys/auth")
public class AuthorityController {

    @Resource
    private AuthorityService authService;
    @Resource
    RoleService roleService;
    @Resource
    AuthorityService authorityService;
    @Resource
    UserService userService;
    @Resource
    UserRoleService userRoleService;
    @Resource
    RoleAuthorityService roleAuthorityService;

    @RequestMapping(value = "list")
    public String index(Model model, HttpServletRequest request, Long roleId){
        User userInfo = (User) request.getSession().getAttribute("userInfo");
        UserRole userRole = userRoleService.findByUserId(userInfo.getId());
        List<Role> roleList = roleService.selectAll();
        model.addAttribute("roleList",roleList);
        return "sys/authority/list";
    }

    @RequestMapping("getMenuTree")
    @ResponseBody
    public String getMenuTree(HttpServletRequest request,Long roleId){
        List<Authority> authList = authorityService.getAllFirstAuth();
        List<ZtreeVO> authorityVOList = new ArrayList<>();
        for (Authority auth : authList) {
            ZtreeVO zv = new ZtreeVO();
            zv.setId(auth.getId().toString());
            zv.setName(auth.getAuthName());
            zv.setpId("0");
            zv.setParent(true);
            zv.setOpen(false);
            zv.setTrueId(auth.getId());
            List<Authority> childList = authorityService.queryForParentId(auth.getId());
            authorityVOList.add(zv);
            for (Authority au : childList) {
                ZtreeVO childTree = new ZtreeVO();
                childTree.setId(au.getId().toString());
                childTree.setName(au.getAuthName());
                childTree.setpId(auth.getId().toString());
                childTree.setTrueId(au.getId());
                List<Authority> chList = authorityService.queryForParentId(au.getId());
                authorityVOList.add(childTree);
                for (Authority ao : chList) {
                    ZtreeVO zvo = new ZtreeVO();
                    zvo.setId(ao.getId().toString());
                    zvo.setName(ao.getAuthName());
                    zvo.setpId(au.getId().toString());
                    zvo.setTrueId(ao.getId());
                    authorityVOList.add(zvo);
                }
            }
        }

        Gson gson=new Gson();
        String json=gson.toJson(authorityVOList);
        return json;
    }

    @RequestMapping("contextList")
    public String userList(@RequestParam(defaultValue = "1")Integer pageNo, Long roleId, String uNameOrNum, HttpServletRequest request){
        User userInfo = (User) request.getSession().getAttribute("userInfo");
        UserRole userRole = userRoleService.findByUserId(userInfo.getId());
        Page page=new Page(pageNo,10);
        if(!LangUtils.isEmpty(uNameOrNum)){
            page.addCond("uNameOrNum","%"+uNameOrNum+"%");
        }
        if(roleId == null){
            page.addCond("roleId",userRole.getRoleId());
        }else {
            page.addCond("roleId",roleId);
        }
        userService.findByRoleId(page);
        request.setAttribute("page",page);
        request.setAttribute("roleId",roleId);
        return "sys/authority/contextlist";
    }

    @RequestMapping("getTree")
    @ResponseBody
    public String getTree(HttpServletRequest request,Long roleId){
        User userInfo = (User) request.getSession().getAttribute("userInfo");
        UserRole userRole = userRoleService.findByUserId(userInfo.getId());
        JSONObject obj = new JSONObject();
        if(roleId == null){
            List<RoleAuthority> roleAuthority = roleAuthorityService.findByRoleId(userRole.getRoleId());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < roleAuthority.size(); i++) {
                sb.append(roleAuthority.get(i).getAuthorityId()+",");
            }
            if(roleAuthority.size() != 0){
                obj.put("checkedNodes",sb.substring(0,sb.lastIndexOf(",")));
            }
        }else{
            List<RoleAuthority> roleAuthority = roleAuthorityService.findByRoleId(roleId);
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < roleAuthority.size(); i++) {
                sb.append(roleAuthority.get(i).getAuthorityId()+",");
            }
            if(roleAuthority.size() != 0){
                obj.put("checkedNodes",sb.substring(0,sb.lastIndexOf(",")));
            }
        }
        return obj.toString();
    }

    @RequestMapping("save")
    @ResponseBody
    public String save(RoleAuthority ra,String authorityIds,HttpServletRequest request){
        JSONObject obj = new JSONObject();
        try {
            if (ra != null) {
                roleAuthorityService.save(ra,authorityIds);
                obj.accumulate("flag",1);
            }
        }catch (Exception e){
            obj.accumulate("flag",0);
        }
        return obj.toString();
    }



}
